﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace ConsolePatient
{

    class Test
    {
        int fieldTop;
        int fieldLeft;
        int fieldLeght;
        public string str = "";
        int currentPos;
        public Test(int fieldTop, int fieldLeft, int fieldLeght)
        {
            this.fieldTop = fieldTop;
            this.fieldLeft = fieldLeft;
            this.fieldLeght = fieldLeght;

            Console.SetCursorPosition(fieldLeft, fieldTop);
            Console.Write(new string('_', fieldLeght));
        }
        public void Set(string str2)
        {
            str = str2;
            Console.CursorLeft = fieldLeft;
            Console.Write(str);
            currentPos = str.Length;
        }
        public ConsoleKey Edit()
        {
            Console.SetCursorPosition(fieldLeft, fieldTop);
            while (true)
            {
                Console.SetCursorPosition(fieldLeft + currentPos, fieldTop);
                ConsoleKeyInfo key = Console.ReadKey(true);
                if (char.IsLetter(key.KeyChar) || char.IsDigit(key.KeyChar))
                {
                    if (str.Length < fieldLeght)
                    {
                        str += key.KeyChar;
                        Console.Write(key.KeyChar);
                        currentPos++;
                    }
                }
                else if (key.Key == ConsoleKey.LeftArrow)
                {
                    if (Console.CursorLeft > fieldLeft)
                    {
                        Console.CursorLeft--;
                        currentPos--;
                    }
                }
                else if (key.Key == ConsoleKey.RightArrow)
                {
                    if (Console.CursorLeft < fieldLeft + str.Length)
                    {
                        Console.CursorLeft++;
                        currentPos++;
                    }
                }
                else if (key.Key == ConsoleKey.Backspace)
                {
                    if (Console.CursorLeft > fieldLeft)
                    {
                        int index = Console.CursorLeft - fieldLeft;
                        str = str.Remove(index - 1, 1);
                        Console.CursorLeft = fieldLeft;
                        Console.Write(str + "_");
                        currentPos--;
                    }
                }
                else if (key.Key == ConsoleKey.Delete)
                {
                    if (Console.CursorLeft < fieldLeft + str.Length)
                    {
                        int index = Console.CursorLeft - fieldLeft;
                        str = str.Remove(index, 1);
                        Console.CursorLeft = fieldLeft;
                        Console.Write(str + '_');
                        Console.CursorLeft = fieldLeft + index;
                     }
                }
                if (key.Key == ConsoleKey.Escape || key.Key == ConsoleKey.Enter || key.Key == ConsoleKey.F2)
                {
                    return key.Key;
                }
            }
        }
    }
    class Program
    {
        public static List<string> str5 = new List<string>();
        static void Color()
        {
            Console.BackgroundColor = ConsoleColor.DarkGray;
            Console.ForegroundColor = ConsoleColor.Yellow;
            for (int i = 6; i < 20; i += 2)
            {
                Console.SetCursorPosition(0, i);
                Console.Write(new string(' ', 90));
            }
        }
        static void String(List<string> str, int m)
        {
            string s1 = null;
            for (int i = 0; i < str5.Count; i++)
            {
                s1 += str5[i]+' ';
            }
            string[] s2 = s1.Split(' ','\r');
            if (str.Count <= s2.Length)
            {
                for (int i = 0; i < s2.Length; i++)
                {
                    str.Add(s2[i]);
                }
            }
            for (int i = 6, j = 0; i < 20; i += 2, j++)
            {
                Console.SetCursorPosition(1, i);
                Console.Write(s2[j + m]); j++;
                Console.SetCursorPosition(20, i);
                Console.Write(s2[j + m]); j++;
                Console.SetCursorPosition(45, i);
                Console.Write(s2[j + m]); j++;
                Console.SetCursorPosition(68, i);
                Console.Write(s2[j + m]);
            }
        }
        static void Add()
        {
            Console.BackgroundColor = ConsoleColor.Magenta;
            Console.ForegroundColor = ConsoleColor.Black;
            Console.Clear();
            Console.BackgroundColor = ConsoleColor.White;
            for (int i = 3; i < 22; i++)
            {
                Console.SetCursorPosition(15, i);
                Console.Write(new string(' ', 60));
            }
            Console.SetCursorPosition(15, 3);
            Console.Write(new string('*', 60));

            for (int q = 3; q < 22; q++)
            {
                Console.SetCursorPosition(15, q);
                Console.Write('*');
                Console.SetCursorPosition(75, q);
                Console.Write('*');
            }
            Console.SetCursorPosition(15, 22);
            Console.Write(new string('*', 61));
        }
        static void String2()
        {
            Console.SetCursorPosition(18, 8);
            Console.Write("Name: ");
            Console.SetCursorPosition(45, 8);
            Console.Write("SurName:     ");
            Console.SetCursorPosition(18, 15);
            Console.Write("Patronymic: ");
            Console.SetCursorPosition(45, 15);
            Console.Write("DateofBirth: ");
        }
        static void AddString()
        {
            Test[] t = new Test[4];
            t[0] = new Test(8, 30, 15);
            t[1] = new Test(8, 58, 15);
            t[2] = new Test(15, 30, 15);
            t[3] = new Test(15, 58, 15);
            int index = 0;
            string str1 = "";
            while (true)
            {
                ConsoleKey key = t[index].Edit();
                if (key == ConsoleKey.Escape)
                {
                    return;
                }
                if (key == ConsoleKey.F2)
                {
                    for (int i = 0; i < t.Length; i++)
                    {
                        if (i == 0)
                        {
                            str1 += t[i].str;
                        }
                        if (i > 0 && i <= 3)
                        {
                            str1 += " " + t[i].str;
                        }
                        if (i == 3)
                        {
                            str1 += "\r";
                        }
                    }
                    str5.Add(str1);
                    Console.Clear();
                    return;
                }
                index++;
                if (index >= t.Length)
                {
                    index = 0;
                }
            }
        }
        static void EditString(string str2, int p, string[] c)
        {
                string[] s = str2.Split(' ');
                Test[] t = new Test[4];
                t[0] = new Test(8, 30, 15);
                t[0].Set(s[0]);
                t[1] = new Test(8, 58, 15);
                t[1].Set(s[1]);
                t[2] = new Test(15, 30, 15);
                t[2].Set(s[2]);
                t[3] = new Test(15, 58, 15);
                t[3].Set(s[3]);
                int index = 0;
                string str1 = "";
                while (true)
                {
                    ConsoleKey key = t[index].Edit();
                    if (key == ConsoleKey.Escape)
                    {
                        return;
                    }
                    if (key == ConsoleKey.F2)
                    {
                        Console.Clear();
                        for (int i = 0; i < t.Length; i++)
                        {
                            if (i == 0)
                            {
                                str1 += t[i].str;
                            }
                            if (i > 0 && i <= 3)
                            {
                                str1 += " " + t[i].str;
                            }
                        }
                        str5.RemoveAt(p);
                        str5.Insert(p, str1);
                        Console.Clear();
                        return;
                    }
                    index++;
                    if (index >= t.Length)
                    {
                        index = 0;
                    }
            }
        }
        static void Chief(int n, int m,List<string> str)
        {
            //for (int i = 0; i < 100; i++)
            //{
            //    string s = "Name" + i + " SurName Patronymic DateOfBirth\r";
            //    File.AppendAllText(@"C:\Users\Roman\Documents\Visual Studio 2010\Projects\ConsolePatient\ConsolePatient\Patsient.txt", s);
            //}
            Console.CursorTop = 10;
            Console.BackgroundColor = ConsoleColor.Cyan;
            Console.ForegroundColor = ConsoleColor.Black;
            Console.CursorTop = 10;
            Console.Clear();
            Console.SetWindowSize(90, 25);
            Console.WriteLine(new string('_', 90));
            Console.WriteLine("|        NAME      |" + "        SURNAME        |" + "        PATRONYMIC     |" + "          DATE       |");
            Console.WriteLine(new string('_', 90));
            Color();
            Console.SetCursorPosition(0, n);
            Console.BackgroundColor = ConsoleColor.Red;
            Console.Write(new string(' ', 90));
            String(str,  m);
            Console.BackgroundColor = ConsoleColor.Cyan;
            Console.ForegroundColor = ConsoleColor.Black;
            Console.SetCursorPosition(0, 19);
            Console.WriteLine(new string('_', 90));
            Console.WriteLine("|     F1 - Add     DELETE - delete   F2 - EDIT   Esc - exit  TOTAL: {0} <1-7> " + new string(' ', 10) + "|",str.Count);
            Console.WriteLine(new string('_', 90));
        }
        static void Main(string[] args)
        {
            string[] c = File.ReadAllLines(@"C:\Users\Администратор\Documents\Visual Studio 2010\cs-pations\ConsolePatient\ConsolePatient");
            for(int i =0 ;i<c.Length;i++)
            {
                str5.Add(c[i]);
            }
            List<string> str = new List<string>();
            int a = 2;
            int n = 6;
            int m = 0;
            int d = 6;
            int r = 0;
            Chief(n, m, str);
            while (true)
            {
                ConsoleKeyInfo key = Console.ReadKey();
                if (key.Key == ConsoleKey.Escape)
                {
                    File.WriteAllText(@"C:\Users\Администратор\Documents\Visual Studio 2010\cs-pations\ConsolePatient\ConsolePatient", "");
                    for (int i = 0; i < str5.Count; i++)
                    {
                        File.AppendAllText(@"C:\Users\Администратор\Documents\Visual Studio 2010\cs-pations\ConsolePatient\ConsolePatient", str5[i] + "\r");
                    }
                    return;
                }
                if (key.Key == ConsoleKey.Delete)
                {
                    int p = (d - 6) / 2;
                    str5.RemoveAt(p);
                    Color();
                    Chief(n, m,  str);
                }
                if (key.Key == ConsoleKey.F1)
                {
                    Add();
                    String2();
                    AddString();
                    Console.Clear();
                    Chief(n, m,  str);
                }
                if (key.Key == ConsoleKey.F2)
                {
                    if (d == 4)
                    {
                        d += 2;
                    }
                    int  p = (d - 6) / 2;
                    string  str2  = c[p];
                    Add();
                    String2();
                    EditString(str2,p,c);
                    Console.Clear();
                    Chief(n, m, str);
                }
                if (key.Key == ConsoleKey.PageUp)
                {
                    if (r > 6)
                    {
                        r--;
                    }
                    if (r == 6 && n == 6 && m > 0)
                    {
                        m -= 4;
                    }
                    if (n > 6 || n == 6)
                    {
                        if (d > 6)
                        {
                            d -= a;
                        }
                        if (n > 6)
                        {
                            n -= a;
                        }
                        Color();
                        Console.SetCursorPosition(0, n);
                        Console.BackgroundColor = ConsoleColor.Red;
                        Console.Write(new string(' ', 90));
                        String(str,  m);
                    }
                }
                if (key.Key == ConsoleKey.PageDown)
                {
                  
                    if (r < 6)
                    {
                        r++;
                    }
                    if (d >= 18 && n == 18 && d / 2 - 2 < str5.Count)
                    {
                        m += 4;

                    }
                    if (6 <= n && d / 2 - 2 < str5.Count)
                    {
                        d += a;
                        Color();
                        if (n < 18)
                        {
                            n += a;
                        }
                        Console.SetCursorPosition(0, n);
                        Console.BackgroundColor = ConsoleColor.Red;
                        Console.Write(new string(' ', 90));
                        String(str, m);
                    }
                }
                Console.SetCursorPosition(0, 24);
            }
        }
    }
}